//
//  ST_UserInfoReusableView.swift
//  BTVideo
//
//  Created by DouLei on 2019/9/25.
//  Copyright © 2019 DouLei. All rights reserved.
//

import UIKit
import SwifterSwift
import SnapKit
import SDWebImage
import SwiftyUserDefaults
//【弃用】
class ST_UserInfoReusableView: UICollectionReusableView {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.backgroundColor = .white
        
        self.addSubview(user_name_label)
        user_name_label.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(15)
            make.top.equalToSuperview().offset(20)
        }
        
        self.addSubview(ageLabel)
        ageLabel.snp.makeConstraints { (make) in
            make.left.equalTo(user_name_label.snp.right).offset(5)
            make.centerY.equalTo(user_name_label)
        }
        self.addSubview(small_heart_image_view)
        small_heart_image_view.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(15)
            make.top.equalTo(user_name_label.snp.bottom).offset(5)
            make.width.height.equalTo(18)
        }
        self.addSubview(like_me_numbers_label)
        like_me_numbers_label.snp.makeConstraints { (make) in
            make.left.equalTo(small_heart_image_view.snp.right)
            make.centerY.equalTo(small_heart_image_view)
        }
        
        self.addSubview(distance_sub_label)
        distance_sub_label.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(15)
            make.centerY.equalTo(small_heart_image_view)
        }
        
        self.addSubview(vip_mark_image_view)
        vip_mark_image_view.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(15)
            make.top.equalTo(small_heart_image_view.snp.bottom).offset(5)
        }
        
        self.addSubview(user_icon_image_view)
        user_icon_image_view.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(-15)
            make.centerY.equalTo(user_name_label.snp.bottom)
            make.width.height.equalTo(60)
        }
        
        
        self.addSubview(display_tag_btn)
        self.addSubview(display_video_btn)
        display_tag_btn.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(15)
            make.width.equalTo(50)
            make.height.equalTo(30)
            make.bottom.equalToSuperview()
        }
        display_video_btn.snp.makeConstraints { (make) in
            make.left.equalTo(display_tag_btn.snp.right).offset(26)
            make.width.equalTo(50)
            make.height.equalTo(30)
            make.bottom.equalToSuperview()
        }
        
        self.addSubview(video_numbers_label)
        video_numbers_label.snp.makeConstraints { (make) in
            make.left.equalTo(display_video_btn.snp.right).offset(-5)
            make.centerY.equalTo(display_video_btn)
        }
        
        self.addSubview(line)
        line.snp.makeConstraints { (make) in
            make.left.equalTo(display_tag_btn)
            make.bottom.equalTo(display_tag_btn.snp.bottom).offset(-1)
            make.width.equalTo(20)
            make.height.equalTo(2)
        }
        
        
        self.addSubview(roundnessView)
        roundnessView.snp.makeConstraints { (make) in
            make.left.equalTo(display_tag_btn.snp.right)
            make.top.equalTo(display_tag_btn).offset(3)
            make.width.height.equalTo(8)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

//    MARK: - Properties

    fileprivate lazy var user_name_label:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = Color.init(hexString: "#333333")
        control.numberOfLines = 0
        
        control.font = UIFont.systemFont(ofSize: 20, weight: UIFont.Weight.semibold)
        return control
    }()

    fileprivate lazy var ageLabel:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = Color.init(hexString: "#333333")
        control.numberOfLines = 0
        
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.semibold)
        return control
    }()
    fileprivate lazy var small_heart_image_view:UIImageView = {
        let control = UIImageView()
        control.contentMode = UIView.ContentMode.scaleAspectFill
        control.image = ST_ImageUtils.imageName("消息_icon_喜欢_small")
        return control
    }()

    fileprivate lazy var like_me_numbers_label:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = Color.init(hexString: "#333333")
        control.numberOfLines = 0
        control.font = UIFont.systemFont(ofSize: 12, weight: UIFont.Weight.regular)
        return control
    }()
    
    fileprivate lazy var distance_sub_label:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = Color.init(hexString: "#333333")
        control.numberOfLines = 0
        control.font = UIFont.systemFont(ofSize: 12, weight: UIFont.Weight.regular)
        control.isHidden = true
        return control
    }()
    
    fileprivate lazy var vip_mark_image_view:UIImageView = {
        let control = UIImageView()
        control.contentMode = UIView.ContentMode.scaleAspectFill
        control.image = ST_ImageUtils.imageName("IndexItem_icon_会员")
        return control
    }()
    
    lazy var user_icon_image_view:UIImageView = {
        let control = UIImageView()
        control.contentMode = UIView.ContentMode.scaleAspectFill
        control.cornerRadius = 60/2.0
        control.borderColor = Color.init(hexString: "#F2F4F7")
        control.borderWidth = 2
        return control
    }()
    
    fileprivate let selected_font = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.semibold)
    
    fileprivate let normalFont = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.semibold)

    fileprivate lazy var display_tag_btn:UIButton = {
            
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setTitleForAllStates("关于我")
        control.setTitleColorForAllStates(UIColor.init(hexString: "#333333")!)
        control.titleLabel?.font = selected_font
        control.addTarget(self, action: #selector(onclick(sender:)), for: UIControl.Event.touchUpInside)
        control.isSelected = true
        return control
    }()

    fileprivate lazy var display_video_btn:UIButton = {
            
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setTitleForAllStates("视频")
        control.setTitleColorForAllStates(UIColor.init(hexString: "#333333")!)
        control.titleLabel?.font = normalFont
        control.addTarget(self, action: #selector(onclick(sender:)), for: UIControl.Event.touchUpInside)
        
//        control.backgroundColor = Color.random
        
        return control
    }()
    
    fileprivate lazy var video_numbers_label:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = Color.init(hexString: "#999999")
        control.font = UIFont.systemFont(ofSize: 12, weight: UIFont.Weight.regular)
        return control
    }()
    
    
    fileprivate lazy var line:UIView = {
        let control = UIView()
        control.cornerRadius = 2
        control.backgroundColor = Color.init(hexString: "#FEC207")!
        return control
    }()
    
    
    fileprivate lazy var roundnessView:UIView = {
        let control = UIView()
        control.cornerRadius = 8/2.0
        control.borderColor = Color.init(hexString: "#FEC207")
        control.borderWidth = 2
        return control
    }()
    
    fileprivate lazy var last_btn:UIButton = {
        return display_tag_btn
    }()
//    MARK: - bind Model
    var model = ST_BTUserinfo(){
        didSet{
            
            user_icon_image_view.pin_setImage(from: model.avatar.compress_image().url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
            
//            他人 展示地址和距离
            if model.accid != Defaults[.currentUserID]{
                
                distance_sub_label.text = model.distance
                
                like_me_numbers_label.isHidden = true
                small_heart_image_view.isHidden = true
                
                distance_sub_label.isHidden = false
                
            }else{
                
                let lovedStr = model.like_number.string +  "人喜欢了你"
                
                like_me_numbers_label.text = lovedStr
                like_me_numbers_label.isHidden = false
                small_heart_image_view.isHidden = false
                
                distance_sub_label.isHidden = true
            }
            
            user_name_label.text = model.nickname
            
            self.ageLabel.text = model.age.string+"岁"
            
            video_numbers_label.text = "("+model.video_count.string+")"
            
            vip_mark_image_view.isHidden = !model.is_vip
        }
    }
//    MARK: - Event
    var _switchClosure:((Bool)->())?
    
     @objc fileprivate func onclick(sender:UIButton){
        
        if sender.isSelected{
            return
        }
        sender.isSelected = true
        sender.titleLabel?.font = selected_font
        last_btn.titleLabel?.font = normalFont
        last_btn.isSelected = false
        last_btn = sender
        
        
        line.snp.remakeConstraints { (make) in
            if sender == self.display_video_btn{
                make.left.equalTo(sender).offset(10)
            }else{
                make.left.equalTo(sender)
            }
            make.bottom.equalTo(sender.snp.bottom).offset(-1)
            make.width.equalTo(20)
            make.height.equalTo(2)
        }
        
        roundnessView.snp.remakeConstraints { (make) in
            
            if sender == self.display_video_btn{
                make.left.equalTo(video_numbers_label.snp.right)
            }else{
                make.left.equalTo(sender.snp.right)
            }
            make.top.equalTo(sender).offset(3)
            make.width.height.equalTo(8)
        }
        
        _switchClosure?(display_tag_btn.isSelected)
        
    }
}
